package com.symria.system.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;


public class CommonExecute {
	Logger logger = Logger.getLogger(CommonExecute.class);
	public String execute(String cmd){
		   try{
				StringBuffer cmdout = new StringBuffer(); 
				logger.info("convert execute cmd:"+cmd);
				//logger.info("pack cmd:"+cmd);
				if(cmd==null) return "";
				Process process = Runtime.getRuntime().exec(cmd); //执行一个系统命令 
				logger.info("pack info：\n" + readInputSteam(process.getInputStream()));
				logger.info("error info:\n" +readInputSteam(process.getErrorStream()));
				return cmdout.toString().trim(); 
			}catch(Exception e){
				logger.error("execute error:"+e.getMessage());
				e.printStackTrace();
			}
			return "";
	   }
	private String readInputSteam(InputStream fis){
		BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 
		StringBuffer sbf = new StringBuffer();
		try {
			String line = null;
			while ((line = br.readLine()) != null) { 
				sbf.append(line); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sbf.toString();
	}
}
